﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Diagnostics;
using System.Linq;
using System.Text;
using AppLab.Data.Models;
using Wicresoft.AppFlow.Flows;

namespace Wicresoft.AppFlow.Activities
{
    public sealed class Review : StateActivity<FlowState>
    {
        protected override string BookmarkName
        {
            get { return FlowState.PendingReview.ToString(); }
        }

        protected override FlowState ResumeBookmark(NativeActivityContext context, Bookmark bookmark, object value)
        {
            var e = value as tysx_Review;
            Debug.Assert(e != null, "Entity object [tysx_Review] cannot be null");

            using (var dc = PMSEntities.Create())
            {

                var flow = dc.tysx_AppFlow.First(p => p.FlowID == context.WorkflowInstanceId);
                var submit = dc.tysx_Submit.First(p => p.SubmitID == flow.SubmitID);

                // e.tysx_TestTask.tysx_MediaClient = submit.tysx_MediaClient;
                dc.tysx_Review.AddObject(e);

                if (e.tysx_TestTask != null)
                {
                    dc.tysx_TestTask.AddObject(e.tysx_TestTask);
                }
                //dc.tysx_TestTask.AddObject(e.tysx_TestTask);
                dc.SaveChanges();

                submit.ReviewID = e.ReviewID;
                flow.StateID = e.ReviewResultID;
                dc.SaveChanges();
            }

            return (FlowState)e.ReviewResultID;
        }
    }
}
